<!--中转航班-->
<template>
  <el-main>
    <div>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="请输入航班号">
          <el-row>
            <el-col :span="50">
              <el-input style="width: 400px;height: 30px" v-model="form.dfltFlno"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="起始站">
          <el-input style="width: 400px;height: 30px" v-model="form.apcd"></el-input>
        </el-form-item>
        <el-form-item label="中转站">
          <el-input style="width: 400px;height: 30px" v-model="form.transitApcd"></el-input>
        </el-form-item>

        <el-form-item label="日期">
          <el-col :span="13">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.dtime"
              style="width: 200px"
              @change="updateDateTime"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 数据表 -->
    <el-table
                  ref="singleTable"
                  :data="tableData"
                  highlight-current-row
                  style="width: 100%"
                >
                  <el-table-column
                    property="dfltFlno"
                    label="航班号"
                    width="100"
                  >
                  </el-table-column>
                  <el-table-column
                    property="dfltAwcd"
                    label="航空公司"
                    width="200"
                  >
                  </el-table-column>
                  <el-table-column property="dtime" label="日期" width="200">
                  </el-table-column>
                  <el-table-column property="apcd" label="第一站" width="120">
                  </el-table-column>
                  <el-table-column
                    property="fptt"
                    label="第一站时间"
                    width="215"
                  >
                  </el-table-column>
                  <el-table-column property="transitApcd" label="第二站" width="120">
                  </el-table-column> 
                  <el-table-column
                    property="fplt"
                    label="第二站时间"
                    width="215"
                  >
                  </el-table-column>

                  <el-table-column
              label="操作"
              width="150"
              v-if="isManagenment">
              <template #default="scope">
          <el-button type="success" size="small" @click="handleEdit(scope.$index, scope.row)"
            >航班变更</el-button
          >
        </template>
      </el-table-column>

                </el-table>
                <!-- 分页组件 -->
                <el-pagination
                  background
                  layout="prev, pager, next"
                  :total="count"
                  :current-page="page"
                  :pageCount="pageCount"
                  @current-change="changePage"
                >
                </el-pagination>

                <el-dialog v-model="dialogVisible" width="50%">
    <h2 style="text-align: center">{{ dialogTitle }}</h2>
    <br />
    <el-form
      :model="user"
      label-width="120px"
      :size="formSize"
      id="editForm"
      :rules="rules"
      ref="ediForm"
    >
      <el-form-item label="航班号" prop="dfltFlno">
        <el-input v-model="check.dfltFlno" />
      </el-form-item>
      <el-form-item label="航空公司" prop="dfltAwcd">
        <el-input v-model="check.dfltAwcd" />
      </el-form-item>
      <el-form-item label="日期" prop="dtime" >
        <el-input v-model="check.dtime" />  
      </el-form-item>
      <el-form-item label="第一站" prop="apcd" >
        <el-input v-model="check.apcd" />
      </el-form-item>
      <el-form-item label="第一站时间" prop="fptt" >
        <el-input v-model="check.fptt" />
      </el-form-item>
      <el-form-item label="第二站" prop="transitApcd" >
        <el-input v-model="check.transitApcd" />
      </el-form-item>
      <el-form-item label="第二站时间" prop="fplt" >
        <el-input v-model="check.fplt" />
      </el-form-item>
      <el-form-item label="原因" prop="flightStatus" >
        <el-input v-model="check.flightStatus" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary"  @click="confirm">
          确定修改
        </el-button>
      </span>
    </template>
  </el-dialog>
              </el-main>
</template>
    
    <script>
    import { ElMessage, ElMessageBox } from 'element-plus'
    import { mapGetters } from "vuex";
    export default {
      name: "Aairtransit",
      computed: {
    ...mapGetters(['isAdmin']),
    ...mapGetters(['isManagenment']),
    ...mapGetters(['isAuditor']),
    ...mapGetters(['isAuditorAndMangement'])
  },
      data() {
    return {
      dialogVisible: false,
      dialogTitle: "修改航班",
      selectLabel: 0,
      tableDataType: 1,
      called:false,
      formLabelWidth: "120px", //表单文本宽度
      tableData: [{}], //图书分页数据
      count: 0, //记录总数
      page: 1, //当前页码
      pageCount: "", //总页数
      form: {
        dfltFlno: "",
        apcd: "",
        transitApcd: "",
        dtime: "2017-06-01T00:00:00.000Z",
      },
      check:{
        dfltFlno: "",
        dfltAwcd: "",
        dtime: "",
        apcd: "",
        fptt: "",
        transitApcd: "",
        fplt: "",
        flightStatus: "",
      },
    }
  },
    methods: {
        changePage(currentPage) {
            this.page = currentPage;
          switch (this.tableDataType){
        case 1:
          this.fetchData();
          break;
        case 2:
          this.onSubmit();
          break;
      }
      },
      fetchData() {
      const pageNum = this.page;
      const pageSize = 10; // 设置每页显示的数据条数
      this.$axios
        .get("http://localhost:8888/dfdl/getAApno3", { params: { pageNum, pageSize } })
        .then((response) => {
          const result = response.data.data;
          this.count = result.size;
          this.pageCount = Math.ceil(result.total / pageSize);
          this.tableData = result.list;
          this.tableDataType=1;
          console.info(this.tableData);
        })
        .catch((error) => {
          console.error(error);
        });
    },

    onSubmit() {
      console.info(this.form);
      const pageNum = this.page;
      const pageSize = 10;
      const dfltFlno = this.form.dfltFlno;
      const dtime = this.form.dtime;
      const apcd = this.form.apcd;
      const transitApcd = this.form.transitApcd;
      this.$axios
        .post("http://localhost:8888/dfdl/v2/selectAApno3PageByParam",JSON.stringify({
            pageNum,
            pageSize,
            dfltFlno,
            dtime,
            apcd,
            transitApcd
        }),{
          headers: {
            'Content-Type': 'application/json' // 修改Content-Type为JSON
          }
        })
        .then((response) => {
          const result = response.data.data;

          this.count = result.size;
          this.pageCount = Math.ceil(result.total / pageSize);
          this.tableData = result.list;
            if(result.list.length!=0 && !this.called){
            this.tableDataType=2;
            this.$message.success({
              type:"success",
              message:"查询成功"
            })
            this.called=true;
          }
          if(result.list.length==0){
            this.tableDataType=1;
            this.$message.error({
              type:"error",
              message:"查询失败，没有数据"
            })
          }

        });
    },
    handleEdit(index, row) {
      this.dialogVisible=true;
      // console.info(form);
      this.check=row;
    },
    cancel() {
      this.dialogVisible = false;
    },
    confirm(){
      ElMessageBox.confirm(
    '你确认要修改吗',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      this.edit();
      //跳转页面
      setTimeout(() => {
      this.$router.push({ name: 'AairtransitCheck' });
    }, 1000);

    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已经取消修改',
      })
      this.dialogVisible = false;
    })
    },

    edit(){
      const transferCheckAD = this.check;
      console.info(transferCheckAD);

      this.$axios
        .post("http://localhost:8888/dfdlcheck/checkA3", 
        transferCheckAD,
        ).then((response) => {

          if(response.data.status==10000){
            this.$message.success({
              type:"success",
              message:"提交成功，正在前往审核页面"
            })
          }else{
            this.$message.error({
              type:"error",
              message:"修改失败"
            })
          }
        });
        },
    },
       
    mounted() {
    this.fetchData();

  }
    }
</script>
<style scoped>
.btn1 {
  margin-left: 220px;
  margin-top: 15px;
}

.link-top {
  height: 10px;
  background-color: #ebeef5;
  margin-top: 20px;
}
</style>